package com.wf.smartcomm.server.serviceImpl;
import java.math.BigInteger;

import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.wf.smartcomm.server.bean.House;
import com.wf.smartcomm.server.bean.Repair;
import com.wf.smartcomm.server.bean.User;
import com.wf.smartcomm.server.bean.Village;
import com.wf.smartcomm.server.model.Page;
import com.wf.smartcomm.server.service.RepairService;
import com.wf.smartcomm.server.util.ConstantUtils;
import com.wf.smartcomm.server.util.IdUtils;

/** 
 * @author 38884306@qq.com  
 * @version 创建时间：2015-5-20 下午3:43:27 
 * 
 */
@Service
@Transactional
public class RepairServiceImpl implements RepairService{

	@Resource
	private SessionFactory factory;
	
	@Override
	public void save(Repair repair) {
		repair.setObjectId(IdUtils.uuid());
		repair.setCreatedAt(String.valueOf(System.currentTimeMillis()));
		repair.setUpdateAt(repair.getCreatedAt());
		factory.getCurrentSession().persist(repair);
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public void listByUser(User user, Page page) {
		Query query = factory.getCurrentSession().createQuery("select count(*) from Repair where user = :user");
		query.setParameter("user", user);
		Long counts = (Long) query.uniqueResult();
		page.setTotalCount(counts.intValue());
		query = factory.getCurrentSession().createQuery("from Repair repair where repair.user = :user order by repair.updateAt desc");
		query.setParameter("user", user);
		query.setMaxResults(page.getPageSize());
		query.setFirstResult(page.getBeginIndex());
		page.setDataList(query.list());
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public void list(Village village,Page page) {
		Query query = factory.getCurrentSession().createSQLQuery("select count(*) from t_repair where hid in (select objectId from t_house where vid =:vid)");
		query.setParameter("vid", village.getObjectId());
		BigInteger counts = (BigInteger) query.uniqueResult();
		page.setTotalCount(counts.intValue());
		query = factory.getCurrentSession().createSQLQuery("select * from t_repair where hid in (select objectId from t_house where vid =:vid) order by updateAt desc").addEntity(Repair.class);
		query.setParameter("vid", village.getObjectId());
		query.setMaxResults(page.getPageSize());
		query.setFirstResult(page.getBeginIndex());
		page.setDataList(query.list());
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public Repair getById(String objectId) {
		Query query = factory.getCurrentSession().createQuery("from Repair where objectId = :objectId");
		query.setParameter("objectId", objectId);
		return (Repair) query.uniqueResult();
	}

	@Override
	public void update(Repair repair) {
		Query query =factory.getCurrentSession().createQuery("update Repair set status = :status where objectId = :id");
		query.setParameter("status", ConstantUtils.REPAIR_STATUS_PROCESSING);
		query.setParameter("id", repair.getObjectId());
		query.executeUpdate();
	}

	@Override
	public void listByKey(Village village,String sSearch, Page page) {
		String hql = "select count(*) from t_repair where hid in (select objectId from t_house where vid =:vid) and description like :key";
		Query query = factory.getCurrentSession().createSQLQuery(hql);
		query.setParameter("vid", village.getObjectId());
		query.setParameter("key", "%"+sSearch+"%");
		BigInteger counts = (BigInteger) query.uniqueResult();
		page.setTotalCount(counts.intValue());
		hql = "select * from t_repair where hid in (select objectId from t_house where vid =:vid) and description like :key order by updateAt desc";
		query = factory.getCurrentSession().createSQLQuery(hql).addEntity(Repair.class);
		query.setParameter("vid", village.getObjectId());
		query.setParameter("key", "%"+sSearch+"%");
		query.setFirstResult(page.getBeginIndex());
		query.setMaxResults(page.getPageSize());
		page.setDataList(query.list());
	}

	@Override
	public void endRepair(String repairId) {
		String sql = "update t_repair set status = 2 where objectId =:rid";
		Query query = factory.getCurrentSession().createSQLQuery(sql);
		query.setParameter("rid", repairId);
		query.executeUpdate();
	}

	@Override
	public void listByHouse(House house, Page page) {
		Query query = factory.getCurrentSession().createQuery("from Repair where house =:house order by createdAt desc");
		query.setParameter("house", house);
		query.setMaxResults(page.getPageSize());
		query.setFirstResult(page.getBeginIndex());
		page.setDataList(query.list());
	}
}
